﻿using AutoMapper;
using Coldairarrow.Api;
using Coldairarrow.Api_Dev.Controllers.Protobuf_Test.DTOModels;
using Coldairarrow.Business.Base_Manage;
using Coldairarrow.Entity;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zaabee.Protobuf;

namespace Coldairarrow.Api_Dev.Controllers.Protobuf_Test
{
    [Route("/Protobuf_Test/[controller]/[action]")]
    [OpenApiTag("Protobuf_User")]
    public class Protobuf_UserController : BaseApiController
    {
        #region DI

        public Protobuf_UserController(IBase_UserBusiness userBus, IMapper mapper)
        {
            _userBus = userBus;
            _mapper = mapper;
        }

        IBase_UserBusiness _userBus { get; }
        IMapper _mapper { get; }
        #endregion

        #region 获取

        [NoFormatResponse]
        [HttpPost]
        public async Task<AjaxResult_Protobuf> GetDataList(PageInput_Protobuf input)
        {
            var result = await _userBus.GetDataListAsync(input.ChangeType<PageInput<Base_UsersInputDTO>>());
            return new AjaxResult_Protobuf()
            {
                Success = result.Success,
                ErrorCode = result.ErrorCode,
                Msg = result.Msg,
                Total = result.Total,
                Data = result.Data == null? null : result.Data.ChangeType<List<Protobuf_UserDTO>>().ToBytes(),
            };
        }

        [NoFormatResponse]
        [HttpPost]
        public async Task<AjaxResult_Protobuf> GetTheData(IdInputDTO_Protobuf input)
        {
            var data = await _userBus.GetTheDataAsync(input.id);
            return new AjaxResult_Protobuf()
            {
                Success = data == null ? false : true,
                Data = data == null ? null : data.ChangeType<Protobuf_UserDTO>().ToBytes(),
            };
        }
        #endregion

        #region 提交

        [NoFormatResponse]
        [HttpPost]
        public async Task<AjaxResult_Protobuf> SaveData(Protobuf_UserDTO input)
        {
            if (!input.Password.IsNullOrEmpty() && !input.Password.IsMd5())
            {
                input.Password = input.Password.ToMD5String();
            }

            if (input.Id.IsNullOrEmpty())
            {
                InitEntity(input);

                await _userBus.AddDataAsync(input.ChangeType<UserEditInputDTO>());
            }
            else
            {
                UpdateEntity(input);

                await _userBus.UpdateDataAsync(input.ChangeType<UserEditInputDTO>());
            }

            AjaxResult_Protobuf res = new AjaxResult_Protobuf
            {
                Success = true,
                Msg = "请求成功！",
            };

            return res;
        }

        [NoFormatResponse]
        [HttpPost]
        public async Task<AjaxResult_Protobuf> DeleteData(Delete_Protobuf ids)
        {
            await _userBus.DeleteDataAsync(ids.ids);

            AjaxResult_Protobuf res = new AjaxResult_Protobuf
            {
                Success = true,
                Msg = "请求成功！",
            };

            return res;
        }

        #endregion
    }
}